package com.sunshine.student_register.ui.activity.manager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.sunshine.student_register.R;
import com.sunshine.student_register.adapter.RecyclerAdapter;
import com.sunshine.student_register.database.register.RegisterEntity;
import com.sunshine.student_register.database.student.StudentEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 信息管理终端
 */
public class ManagerActivity extends AppCompatActivity implements View.OnClickListener{

    private ManagerViewModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manager);

        viewModel = new ViewModelProvider(this).get(ManagerViewModel.class);
        getSupportActionBar().setTitle("信息管理终端");

        initView();
    }

    private void initView() {
        Button allStudent = findViewById(R.id.button_allstudent);
        Button allRegister = findViewById(R.id.button_allregister);
        Button deleteStudent = findViewById(R.id.button_deletestudent);
        Button deleteRegister = findViewById(R.id.button_deleteregister);

        allStudent.setOnClickListener(this);
        allRegister.setOnClickListener(this);
        deleteRegister.setOnClickListener(this);
        deleteStudent.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_allstudent:
                viewModel.getStudentLiveData().observe(this, new Observer<ArrayList<StudentEntity>>() {
                    @Override
                    public void onChanged(ArrayList<StudentEntity> studentEntities) {
                        if (null != studentEntities){
                            RecyclerView recyclerView = findViewById(R.id.recycler);
                            recyclerView.setAdapter(new StudentRecyclerAdapter(studentEntities, new CallBack() {
                                @Override
                                public <T, S> void callBack(T bean, S bean1) {
                                    switch ((Integer) bean1){
                                        case 0:
                                            viewModel.updateStudent((StudentEntity) bean);
                                            Toast.makeText(ManagerActivity.this, "模拟修改成功", Toast.LENGTH_SHORT).show();
                                            break;
                                        case 1:
                                            Toast.makeText(ManagerActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                                            viewModel.deleteStudent((StudentEntity) bean);
                                            break;
                                    }
                                }
                            }));
                            recyclerView.setLayoutManager(new LinearLayoutManager(ManagerActivity.this));
                        }
                    }
                });
                break;
            case R.id.button_allregister:
                viewModel.getRegisterLiveData().observe(this, new Observer<ArrayList<RegisterEntity>>() {
                    @Override
                    public void onChanged(ArrayList<RegisterEntity> registerEntities) {
                        if (null != registerEntities || registerEntities.size() > 0){
                            RecyclerView recyclerView = findViewById(R.id.recycler);
                            recyclerView.setAdapter(new RecyclerAdapter(registerEntities));
                            recyclerView.setLayoutManager(new LinearLayoutManager(ManagerActivity.this));
                        }
                    }

                });
                break;
            case R.id.button_deletestudent:
                viewModel.deleteAllStudent();
                Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
                break;
            case R.id.button_deleteregister:
                viewModel.deleteAllRegister();
                Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
